package com.yegs.jltqc.serviceImpl;

import com.yegs.jltqc.TO.NamePageQuery;
import com.yegs.jltqc.VO.QcProduce;
import com.yegs.jltqc.VO.WorkFlow;
import com.yegs.jltqc.config.ConcurentCache;
import com.yegs.jltqc.dao.QcProduceRepository;
import com.yegs.jltqc.dao.WorkFlowRepository;
import com.yegs.jltqc.service.QcProduceService;
import com.yegs.jltqc.service.WorkFlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class WorkFlowServiceImpl implements WorkFlowService {

    @Autowired
    private WorkFlowRepository workFlowRepository;

    @Override
    public WorkFlow save(WorkFlow workFlow) {
        workFlow.setCreateTime(new Date());
        workFlow.setCreateBy(ConcurentCache.getUserName());
        return workFlowRepository.save(workFlow);
    }

    @Override
    public List<WorkFlow> list() {
        return workFlowRepository.findAll();
    }

    @Override
    public WorkFlow get(Long id) {
        return workFlowRepository.getOne(id);
    }

    @Override
    public void del(Long id) {
        workFlowRepository.deleteById(id);
    }

    @Override
    public Page<WorkFlow> query(NamePageQuery query) {
        PageRequest request = PageRequest.of(query.getPageIndex() - 1, query.getPageSize());
        if(query.getName() == null || query.getName().trim() == ""){
            return workFlowRepository.findWorkFlowsByStatus(WorkFlow.STATUS_ING, request);
        }
        return workFlowRepository.findWorkFlowByWorkNameAndStatus(query.getName(), WorkFlow.STATUS_ING, request);
    }
    @Override
    public Page<WorkFlow> queryAll(NamePageQuery query) {
        PageRequest request = PageRequest.of(query.getPageIndex() - 1, query.getPageSize());
        if(query.getName() == null || query.getName().trim() == ""){
            return workFlowRepository.findAll(request);
        }
        return workFlowRepository.findWorkFlowByWorkName(query.getName(), request);
    }

    @Override
    public Long getNext(Long id) {
        if(id == null){
            WorkFlow workFlow = workFlowRepository.findFirstByOrderByIdDesc();
            if(workFlow != null) {
                return workFlow.getId();
            }
        }
        WorkFlow workFlow = workFlowRepository.findFirstByIdIsAfter(id);
        if(workFlow == null){ workFlow = workFlowRepository.findFirstByOrderByIdDesc(); }
        if(workFlow != null){
            return workFlow.getId();
        }
        return null;
    }

    @Override
    public Long getLast(Long id) {
        if(id == null){
            WorkFlow workFlow = workFlowRepository.findFirstByOrderByIdDesc();
            if(workFlow != null) {
                return workFlow.getId();
            }
        }
        else{
            WorkFlow workFlow = workFlowRepository.findFirstByIdBeforeOrderByIdDesc(id);
            if(workFlow == null){ workFlow = workFlowRepository.findFirstByOrderById(); }
            if(workFlow != null){
                return workFlow.getId();
            }
        }
        return null;
    }
}
